<?php
namespace application\admin\controllers;

use mz\ars\Site;
use Yii;
use mz\controllers\AdminController as Controller;

class SiteController extends Controller
{
    # 显示网站的配置信息[按照id倒序排 只取一条]
    public function actionIndex()
    {
        $config = Site::getFindOne();

        if (!empty($config['logo']))$config['logo'] = 'http://img.mzcms.com/'.$config['logo'];
        return $this->render('index',compact('config'));
    }

    # 报存更改
    public function actionSave()
    {
        if (!Yii::$app->request->isAjax) return $this->ajaxReturn(['e' => '2','m' => '请使用正确的请求方式']);
        $params = Yii::$app->request->post();
        $saveData = array_filter($params,function($key){
            return in_array($key , ['id','title','desc','icp','logo']);
        },ARRAY_FILTER_USE_KEY);

        $model = !empty($saveData['id']) ? Site::findOne(['id' => $saveData['id']]) : new Site();
        $model->attributes = $saveData;
        # 数据验证
        if (!$model->validate()){
            var_dump($model->getErrors());
            return $this->ajaxReturn(['e' => 1,'m' => "很遗憾，数据验证失败！"]);
        }
        # 执行保存
        $msg = $model->save() ? ['e' => 0,'m' => "恭喜您，操作成功！"] : ['e' => '1','很遗憾，操作失败'];
        return $this->ajaxReturn($msg);

    }
}